package com.qf.shiro.user.controller;

import com.qf.shiro.user.entity.User;
import com.qf.shiro.user.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljc
 * @since 2023-02-08
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/register")
    public String  regist(User user){

        userService.regist(user);

        return "redirect:/login.html";
    }
    @PostMapping("/login")
    public String login(String username,String password,boolean rememberMe) {

        Subject subject = SecurityUtils.getSubject();
        //设置免登录
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password,rememberMe);

        subject.login(usernamePasswordToken);

        return "redirect:/home.html";
    }

    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        return "redirect:/login.html";
    }
}
